using System;
using System.Threading;
using GomokuGame.AI;

namespace GomokuGame
{
    /// <summary>
    /// AI游戏测试程序
    /// </summary>
    public class AITestGame
    {
        private GameEngine gameEngine;
        private AIManager aiManager;

        public AITestGame()
        {
            gameEngine = new GameEngine();
            aiManager = new AIManager();
        }

        public void TestAIVsAI()
        {
            Console.WriteLine("=== AI vs AI 测试 ===");
            
            // 设置AI难度
            aiManager.SetAIDifficulty(AIDifficulty.Intermediate);
            gameEngine.SetAIDifficulty(AIDifficulty.Intermediate);
            
            // 开始AI vs AI游戏
            gameEngine.StartNewGame(GameMode.AIVsAI, AIDifficulty.Intermediate, PieceType.White);
            
            Console.WriteLine("游戏开始！");
            Console.WriteLine($"当前玩家: {(gameEngine.CurrentPlayer == PieceType.Black ? "黑" : "白")}");
            
            // 绑定事件
            gameEngine.PiecePlaced += OnPiecePlaced;
            gameEngine.GameEnded += OnGameEnded;
            gameEngine.AIMoveCompleted += OnAIMoveCompleted;
            
            // 等待游戏结束
            while (gameEngine.CurrentState == GameState.Playing)
            {
                Thread.Sleep(100);
            }
            
            Console.WriteLine("游戏结束！");
        }

        private void OnPiecePlaced(object sender, MoveEventArgs e)
        {
            Console.WriteLine($"落子: {e.Move.PieceType} 在 ({e.Move.Row}, {e.Move.Col})");
            PrintBoard();
        }

        private void OnAIMoveCompleted(object sender, AIMoveCompletedEventArgs e)
        {
            Console.WriteLine($"AI移动完成: {e.Move.PieceType} 在 ({e.Move.Row}, {e.Move.Col})");
        }

        private void OnGameEnded(object sender, GameEventArgs e)
        {
            Console.WriteLine($"游戏结束: {e.GameState}");
        }

        private void PrintBoard()
        {
            Console.WriteLine("当前棋盘状态:");
            Console.WriteLine("  0 1 2 3 4 5 6 7 8 9 1011121314");
            
            for (int row = 0; row < 15; row++)
            {
                Console.Write($"{row,2}");
                for (int col = 0; col < 15; col++)
                {
                    var piece = gameEngine.GetPiece(row, col);
                    char symbol;
                    switch (piece)
                    {
                        case PieceType.Empty:
                            symbol = '.';
                            break;
                        case PieceType.Black:
                            symbol = '●';
                            break;
                        case PieceType.White:
                            symbol = '○';
                            break;
                        default:
                            symbol = '?';
                            break;
                    }
                    Console.Write($" {symbol}");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }

        public static void Test()
        {
            var test = new AITestGame();
            test.TestAIVsAI();
        }
    }
} 